package com.windfone.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.common.Constant;
import com.windfone.common.PageUtil;
import com.windfone.model.Gift;
import com.windfone.model.Member;
import com.windfone.service.GiftService;
import com.windfone.service.MemberService;

public class GiftAction extends ActionSupport {
	private GiftService giftService;
	private MemberService memberService;
	private List<Gift> giftList;
	private Gift bean;
	private int beginRecord;
	private int currentPage;
	private int nextPage;
	private int totalPage;
	private int prevPage;
	private int giftId;
	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}
	public int getGiftId() {
		return giftId;
	}
	public void setGiftId(int giftId) {
		this.giftId = giftId;
	}
	 
	public List<Gift> getGiftList() {
		return giftList;
	}
	public void setGiftList(List<Gift> giftList) {
		this.giftList = giftList;
	}
	public void setGiftService(GiftService giftService) {
		this.giftService = giftService;
	}
	
	public Gift getBean() {
		return bean;
	}
	public void setBean(Gift bean) {
		this.bean = bean;
	}
	public int getBeginRecord() {
		return beginRecord;
	}
	public void setBeginRecord(int beginRecord) {
		this.beginRecord = beginRecord;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getNextPage() {
		return nextPage;
	}
	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getPrevPage() {
		return prevPage;
	}
	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}
	public GiftService getGiftService() {
		return giftService;
	}

	
	@Override
	public String execute() throws Exception {
		String whereCondition = "";
		PageUtil pu = new PageUtil();
		pu.setCurrentPage(currentPage);
		int totalRecords = giftService.getAllRecorderNumber(whereCondition) ;
		pu.setTotalRecords( totalRecords );
		pu.setPageSize(Constant.PAGE_SIZE);
		pu.dealPage();
		currentPage = pu.getCurrentPage();
		nextPage = pu.getNextPage();
		totalPage = pu.getTotalPage();
		prevPage = pu.getPrevPage();
		
		beginRecord = Constant.PAGE_SIZE * (currentPage -1);
		if ( beginRecord< 0 )
			beginRecord = 0; 
		giftList = giftService.getAllGift(beginRecord,whereCondition);
		return SUCCESS;
	}
	 
	public String giftAsk() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("userBean");
		if ( obj !=null ){
			Gift f= new Gift();
			f.setId(giftId);
			bean = giftService.getGift(f);
			if ( bean.getAccount()>0){
				Member bean2= (Member)obj;
				giftService.addUserGift(giftId, bean2.getId());
				/*bean2 = memberService.getMemberByKey(bean2.getId());
				int p = bean2.getPoints();
				p=p- bean.getJifen();
				bean2.setPoints(p);
				memberService.updateMember(bean2);*/
			}
		}
		return SUCCESS;
	}
	
	
	public String giftDetail() throws Exception{
		Gift f= new Gift();
		f.setId(giftId);
		bean = giftService.getGift(f);
		return SUCCESS;
	}
	public String giftList() throws Exception {
		String whereCondition = "";
		PageUtil pu = new PageUtil();
		pu.setCurrentPage(currentPage);
		int totalRecords = giftService.getAllRecorderNumber(whereCondition) ;
		pu.setTotalRecords( totalRecords );
		pu.setPageSize(Constant.PAGE_SIZE);
		pu.dealPage();
		currentPage = pu.getCurrentPage();
		nextPage = pu.getNextPage();
		totalPage = pu.getTotalPage();
		prevPage = pu.getPrevPage();
		
		beginRecord = Constant.PAGE_SIZE * (currentPage -1);
		if ( beginRecord< 0 )
			beginRecord = 0; 
		giftList = giftService.getAllGift(beginRecord,whereCondition);
		return SUCCESS;
	}

}
